const net = require('net')



let socket = net.connect('9876', '192.168.43.49')

socket.on('error', function() {
	console.log('[socket:onerror]', arguments)
})

socket.on('connect', function() {
	console.log('[socket:onconnect]', arguments)
	let headerBuffer = Buffer.from('{"code":105,"extFields":{"topic":"TBW102"},"flag":0,"language":"JAVA","opaque":0,"serializeTypeCurrentRPC":"JSON","version":397}')
	let headerLength = headerBuffer.length
	let totalLength = headerLength + 4
	socket.write(Buffer.concat([
		Buffer.from(totalLength.toString(16).padStart(8,'0'), 'hex'),
		Buffer.from(headerLength.toString(16).padStart(8,'0'), 'hex'),
		headerBuffer
	]))

	socket.on('data', function(data) {
		console.log('[socket:ondata]', data.toString('utf-8'))
		console.log('[socket:ondata]', data.toString('base64'))
		console.log('[socket:ondata]', data.toString('hex'))
		console.log('\n')

		let totalLength = parseInt(data.slice(0,4).toString('hex'), 16)
		let headerLength = parseInt(data.slice(4,8).toString('hex'), 16)
		let headerJSON = JSON.parse(data.slice(8).toString().slice(0, headerLength))
		let bodyJSON = new Function('return ' + data.slice(8).toString().slice(headerLength))()
		let brokerAddr = bodyJSON.brokerDatas[0].brokerAddrs['0'];

		console.log('[mqserver] totalLength: ', totalLength)
		console.log('[mqserver] headerLength: ', headerLength)
		console.log('[mqserver] headerJSON: ', JSON.stringify(headerJSON, null, 2))
		console.log('[mqserver] bodyJSON: ', JSON.stringify(bodyJSON, null, 2))
		console.log('[mqserver] brokerAddr: ', brokerAddr)

		socket.end()
		socket.removeAllListeners()

		let brokerAddrArr = brokerAddr.split(':')
		socket = net.connect(brokerAddrArr[1], brokerAddrArr[0])

		socket.on('connect', function() {

			let headerBuffer = Buffer.from(`{"code":310,"extFields":{"a":"please_rename_unique_group_name","b":"TopicTest","c":"TBW102","d":"4","e":"5","f":"0","g":"${Date.now()}","h":"0","i":"UNIQ_KEY\\u0001240989542C5424C80804C254E01303C1649420AA3C1A214E962D0000\\u0002WAIT\\u0001true\\u0002TAGS\\u0001TagA","j":"0","k":"false","m":"false"},"flag":0,"language":"JAVA","opaque":0,"serializeTypeCurrentRPC":"JSON","version":397}`)
			let headerLength = headerBuffer.length
			let bodyBuffer = Buffer.from('Hello MQ')
			let bodyLength = bodyBuffer.length
			let totalLength = 4 + headerLength + bodyLength

			socket.write(Buffer.concat([
				Buffer.from(totalLength.toString(16).padStart(8,'0'), 'hex'),
				Buffer.from(headerLength.toString(16).padStart(8,'0'), 'hex'),
				headerBuffer,
				bodyBuffer
			]))


			socket.on('data', (data) => {
				console.log('[broker:ondata]', data.toString('utf-8'))
				console.log('[broker:ondata]', data.toString('base64'))
				console.log('[broker:ondata]', data.toString('hex'))
				console.log('\n')

				socket.end()
				socket.removeAllListeners()
			})

			
		})

	})

})

socket.on('end', function() {
	console.log('[socket:onend]', arguments)
})
socket.on('close', function() {
	console.log('[socket:onclose]', arguments)
})

